package com.mxchip.livestarmobile.mobile.av.player;

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;

import com.mxchip.livestarmobile.mobile.av.screen.ScreenSettings;
import com.mxchip.livestarmobile.mobile.av.screen.VideoGestureListener;
import com.mxchip.livestarmobile.mobile.util.Log.MyLog;
import com.mxchip.hxb3288a.av.screen.CameraTouchControl;

import org.jetbrains.annotations.NotNull;


public class HxbPlayerView extends GLSurfaceView {

    private static final String TAG = "HxbPlayerView";
    private HxbSurfaceRender render;
    private CameraTouchControl control;

    public HxbPlayerView(Context context) {
        this(context, null);
    }

    public HxbPlayerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setEGLContextClientVersion(2);

        render = new HxbSurfaceRender(context);
        setRenderer(render);
        setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);


        control = new CameraTouchControl(this);
        control.setVideoGestureListener(new MyVideoGestureListener());

        // render.setRequestRenderInvoke(this::requestRender);
    }

    public void setScreenSettings(ScreenSettings screenSettings) {
        Log.e("HxbPlayerView", "render.setScreenSettings render: " + render + ", screenSettings: " + screenSettings);
        if (render != null) {
            render.setScreenSettings(screenSettings);
        }
    }

    public void decodeAVPacket(int size, @NotNull byte[] data, @NotNull String decoderName) {
        if (render != null) {
            render.decodeAVPacket(size, data, decoderName);
        }
    }


    class MyVideoGestureListener implements VideoGestureListener {
        @Override
        public void onVolumeGesture(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            if (videoGestureListener != null) {
                videoGestureListener.onVolumeGesture(e1, e2, distanceX, distanceY);
            }
        }

        @Override
        public void onSingleTapGesture(MotionEvent e) {
            if (videoGestureListener != null) {
                videoGestureListener.onSingleTapGesture(e);
            }
        }

        @Override
        public void onDown(MotionEvent e) {
            if (videoGestureListener != null) {
                videoGestureListener.onDown(e);
            }
        }

        @Override
        public void onSelectSomeOne(int index) {
            if (videoGestureListener != null) {
                videoGestureListener.onSelectSomeOne(index);
            }
        }

        @Override
        public void onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            if (videoGestureListener != null) {
                videoGestureListener.onFling(e1, e2, velocityX, velocityY);
            }
        }

    }

    /*private void initAudio() {
        if (onSurfaceCreatedListener != null)
            onSurfaceCreatedListener.onSurfaceCreated();
    }*/

    public void onDestroy() {
        Log.e(TAG, "HxbPlayerView onDestroy");

        if (onSurfaceCreatedListener != null) {
            onSurfaceCreatedListener = null;
        }

        if (render != null) {
            render.release();
            render = null;
        }

        if (control != null) {
            control.release();
            control = null;
        }

        if (videoGestureListener != null) {
            videoGestureListener = null;
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        MyLog.logTime("HxbPlayerView:surfaceCreated: ");
        super.surfaceCreated(holder);

        if (onSurfaceCreatedListener != null)
            onSurfaceCreatedListener.onSurfaceCreated();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        super.surfaceChanged(holder, format, w, h);
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        super.surfaceDestroyed(holder);
        onDestroy();
    }


    /**
     * ResourcesListener
     */
    private OnSurfaceCreatedListener onSurfaceCreatedListener;

    public void setOnSurfaceCreatedListener(OnSurfaceCreatedListener onSurfaceCreatedListener) {
        this.onSurfaceCreatedListener = onSurfaceCreatedListener;
    }

    private VideoGestureListener videoGestureListener;

    public void setVideoGestureListener(VideoGestureListener videoGestureListener) {
        this.videoGestureListener = videoGestureListener;
    }

}
